﻿
define([
'Wjs/Wjs'
],
function () {
    'use strict';

    var type = 'CollapserHandler', // Use the type registration to avoid recreating the type
    typeManager = Wjs.typeManager,
    CollapserHandler = typeManager.getType(type);

    if (CollapserHandler) { // It has been already loaded
        return CollapserHandler;
    }

    // Renders the html from a component
    CollapserHandler = Wjs.typeBuilder.define({

        type: type, // Set the type

        // Call it after calling Widget construct
        initialize: function (conf) {
            conf = conf || {};

            this.onCollapse = Wjs.chain(conf.onCollapse, this.onCollapse); // Override this onCollapse with the one of the configuration
            this.onExpand = Wjs.chain(conf.onExpand, this.onExpand); // Override this on onExpand with the one of the configuration
        },

        onCollapse: function () {
            var owner = this.owner;

            if (owner && owner.onCollapse) { // Propagate to owner
                owner.onCollapse.call(owner, this);
            }
        },

        onExpand: function () {
            var owner = this.parent;

            if (owner && owner.onExpand) { // Propagate to owner
                owner.onExpand.call(owner, this);
            }
        }
    });

    typeManager.registerType(CollapserHandler);

    return CollapserHandler;
});